package com.java.comprehensive.arithmetic.programmers;

/**
 * @Author: Dgg
 * @Date: 2022/5/3 21:58
 */
public class CompressString {
	public String compressString(String S) {
		int i = 0, j = 0, size = S.length();
		StringBuilder stringBuffer = new StringBuilder();
		while (i < size) {
			while (j < size && S.charAt(i) == S.charAt(j)) {
				j++;
			}
			stringBuffer.append(S.charAt(i)).append(j - i);
			i = j;
		}

		return S.length() > stringBuffer.length() ? stringBuffer.toString() : S;
	}

	public static void main(String[] args) {
		String S = "aabcccccaaa";
		CompressString compressString = new CompressString();

		String s = compressString.compressString(S);
		System.out.println(s);
	}


}
